if (!Util) {
    var Util = {};
}
/* firefox text-overflow ellipsis plugin */
jQuery.extend({
    isSupportTextoverflow:('textOverflow' in document.documentElement.style || 'OTextOverflow' in document.documentElement.style)
});
/*
 * autoUpdating: auto ellipsis element
 * childrenlevel 0:current element, >1: children element level, undefined: the finally children element
 */
(function($) {
    $.fn.ellipsis = function(autoUpdating,childrenlevel){
        //var s = document.documentElement.style;
		var autoUpdate=autoUpdating||false;
		var existLevel=typeof childrenlevel!='undefined';
        return this.each(function(){
            var el = $(this);
            if (el.css("overflow") == "hidden") {
				var aimDom = el;
				if (!existLevel || childrenlevel >= 0) {
					var level = 0;
					aimDom=el;
					while (aimDom.children().length > 0 && (!existLevel || level < childrenlevel)) {//get the direct wrap of text node 
						aimDom = aimDom.children();
						level++;
					}
				}
				aimDom = aimDom.eq(0);
                var originalText=aimDom.text();
				if(originalText.length==0){
					this.aimDom=null;// add by yuntao to fix  IE bug
                    //delete this.aimDom;
					return;
				}
				var originalHtml;
				if(autoUpdating) originalHtml=el.html();//backup for recover
                var w=aimDom.width();
                var t = aimDom.clone(true).css({'position':'absolute', 'width':'auto', 'overflow':'visible'}).hide();
				aimDom.after(t);
                var text = originalText;
                if (!jQuery.isSupportTextoverflow) {//if browser is FF
//                    if(!aimDom.data("bestpx")){//cache best pixel
//                       aimDom.data("bestpx",t.width());
//                    }
                    if (!aimDom.data("realvalue")) {
	                    aimDom.data("realvalue", originalText);
	                }
                    var temptext=aimDom.data("realvalue");
                    if(originalText.lastIndexOf("...")!=-1){
                        t.text(temptext);
                        text=temptext;
                    }else{
	                    if(originalText!=aimDom.data("realvalue")){//dom content have changed
	                        aimDom.data("realvalue",originalText);
	                        aimDom.data("bestpx",t.width());
	                    }
					}
//					if (t.width() != aimDom.data("bestpx")) {//word-bold
//                       aimDom.data("bestpx",t.width());
//                    }
//					if (aimDom.data("bestpx") > w) {
						var length=text.length;
						while (length > 0 && t.width() > w) {
							text = text.substr(0, --length);
							t.text(text + "...");
						}
//					}
                    aimDom.text(t.text());//change the text context to the ellipsis value
					aimDom.attr("title", aimDom.data("realvalue"));//add the title for the ellipsis node
                }else{
                    if(!aimDom.hasClass("qs-common-ellipsis")||originalText!=aimDom.data("realvalue")){
						aimDom.data("realvalue", originalText);
                        aimDom.addClass("qs-common-ellipsis");
						aimDom.attr("title",originalText);//add the title for the ellipsis node
                    }
                }
                t.remove();
                if (autoUpdate) {
                    var oldW = el.width();//auto refresh the text node 
                    setInterval(function(){
                        if (el.width() != oldW) {
                            oldW = el.width();
                            el.html(originalHtml);
                            el.ellipsis();
                        }
                    }, 200);
                }
            }
        });
    }
})(jQuery);

Util.FixIE = (function(){
    if (document.all) {
        document.execCommand("BackgroundImageCache", false, true);
    }
})();
/*
 * Util.DelayRun('resize', this._resizeBody, 10, this);
 * */
Util.DelayRun = function(timerKey, fn, delay, object){
    if (object.__DelayRunTimer) {
        if (object.__DelayRunTimer[timerKey]) {
            clearTimeout(object.__DelayRunTimer[timerKey]);
            object.__DelayRunTimer[timerKey] = -1;
        }
    }
    else {
        object.__DelayRunTimer = {};
    }
    object.__DelayRunTimer[timerKey] = setTimeout(function(){
        fn.call(object);
    }, delay);
};
Util.getScrollPosition=function(){
	if (typeof(window.pageYOffset) == 'number') {
	    this.getScrollPosition=function(){
            return {scrollx:window.pageXOffset,scrolly:window.pageYOffset};
		}
	}else if (document.documentElement && (typeof document.documentElement.scrollLeft!='undefined' || typeof document.documentElement.scrollTop!='undefined')) {
		this.getScrollPosition=function(){
			return {scrollx:document.documentElement.scrollLeft,scrolly:document.documentElement.scrollTop};
		}
	}else if (document.body && typeof document.body.scrollLeft!='undefined') {
		this.getScrollPosition=function(){
			return {scrollx:document.body.scrollLeft,scrolly:document.body.scrollTop};
		}
	}else{
		return {scrollx:0,scrolly:0};
	}
	return this.getScrollPosition();
}
//contains check f contains c
Util.contains = function(f, c){
    if (f == null) {
        return false;
    }
    var bCB2F = false;
    if (f.contains) {
        bCB2F = f.contains(c);
    }
    else {
        bCB2F = (f.compareDocumentPosition(c) == 20) ? true : false;
    }
    return bCB2F;
};

Util.addBetaTip = function(TextEl, TipEl){
    var el = (TextEl[0] || TextEl)
    el.innerHTML += ' (beta)';
    var timeout = null;
    var tipEl = $("<div class='wstl-options-tip'>This is a new feature that might have a few kinks to work out.</div>").appendTo(document.body);
    
    $(TipEl ? (TipEl[0] || TipEl) : el).bind('mouseover', function(e){
        var targetEl = $(e.target);
        var val = targetEl.parent().attr("value");
        if (timeout != null) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(function(){
            tipEl.show();
            tipEl.css({
                left: e.clientX + 10,
                top: e.clientY + 10
            });
        }, 100);
    }).bind('mouseout', function(){
        if (timeout != null) {
            clearTimeout(timeout);
        }
        tipEl.hide();
    });
};

//panel
Util.panel = function(callback){
    var html = '<div class="rtq-panel">' +
				'<div class="shadow"></div>' +
				'<div class="container">' +
				'<div class="hd"><h4>Title</h4><div class="hd-btn"><a class="close">X</a></div></div>' +
				'<div class="ctn"></div>' +
				'</div>' +				
				'</div>';
    this.jqDOM = $(html);
    this.title = null;
    this.ctn = null;
    this.utilType = "";
    this.closeCallback = callback;
    this._init();
    return this;
};
Util.panel.prototype = {
    _init: function(){
        var self = this;
        this.title = this.jqDOM.find('.hd h4');
        this.ctn = this.jqDOM.find('.ctn');
        this.jqDOM.find('.hd a.close').click(function(){
            self.hide();
            if (typeof self.closeCallback == 'function') {
                self.closeCallback();
            }
        });
        
        this.jqDOM.appendTo(document.body);
    },
    addToContent: function(obj){
        this.ctn.append(obj);
    },
    setTitle: function(str){
        this.title.html(str);
    },
    show: function(){
        this.jqDOM.find('.shadow').css({
            height: this.jqDOM.height()
        });
        this.jqDOM.show();
    },
    hide: function(){// type for utilmessage
        this.jqDOM.hide();
        if (this.utilType != "message") {
            QS_FullMarsk.hide();
        }
    },
    del: function(){
        this.jqDOM.remove();
    },
    setCloseCallback: function(callback){
        this.closeCallback = callback;
    }
};


Util.promptObj = null;
Util.prompt = function(msg, okCallback,config){
    if (!Util.promptObj) {
        var panel = new Util.panel();
        panel.jqDOM.addClass('rtq-prompt rtq-a-c-p');
        var content=(config&&config.label)?'<div class="qs-ui-label">'+config.label+':'+'</div>':"";
        panel.addToContent('<div class="qs-ui-content">'+content+'<input class="qs-ui-ipt name-ipt" type="text" /></div><div class="btn-ctn"><a href="javascript:void(0)" class="qs-ui-btn ok">OK</a><a class="qs-ui-btn cancel" href="javascript:void(0)">Cancel</a></div>');
        panel.setTitle('Confirm');
        panel.okCallback = function(val){
            
        };
        panel.config = {
            validateMethod: function(){
                return true;
            },
            cancelCallBack: null,
            beforeCloseCallBack: null
        }
        panel.input = panel.ctn.find('.name-ipt');
        var btnOK = panel.ctn.find('.ok');
        var btnCancel = panel.ctn.find('.cancel');
        
        var fnSubmit = function(){
            if (!panel.config.validateMethod(panel.input.val())) {
                return false;
            }
            if (typeof(panel.config.beforeCloseCallBack) == "function") {
                if (panel.config.beforeCloseCallBack(panel.input.val())) {
                    panel.hide();
                    panel.okCallback(panel.input.val());
                }
                else {
                    panel.input.val('');
                    panel.input.focus();
                }
            }
            else {
                panel.hide();
                panel.okCallback(panel.input.val());
            }
        };
        
        panel.input.bind('keydown', function(e){
            if (e.keyCode == 13) {
                fnSubmit();
            }
        });
        
        btnOK.click(fnSubmit);
        
        btnCancel.click(function(e){
            if (typeof(panel.config.cancelCallBack) == 'function') {
                panel.config.cancelCallBack();
            }
            panel.hide();
            return false;
        });
        
        Util.promptObj = panel;
        
    }
    
    Util.promptObj.okCallback = okCallback;
    if (config) {
        $.extend(true, Util.promptObj.config, config);
    }
    Util.promptObj.input.val('');
    Util.promptObj.setTitle(msg);
    Util.promptObj.show();
    Util.promptObj.input.focus();
};
//Util.prompt();
/*20120615 kidy refactor for fixing callback coverge bug*/
Util.confirmObj = null;
Util.confirm=function(){
    var cacheRquest={};
    var isLoading=false;
    var doNext=function(){
        for(var msg in cacheRquest){
            show(msg,cacheRquest[msg].callbacks[0],cacheRquest[msg]);
            return;
        }
    };
    var createConfirmObj=function(){
         var panel = new Util.panel();
            panel.jqDOM.addClass('rtq-confirm rtq-a-c-p');
            panel.addToContent('<h5>Message</h5><div class="btn-ctn"><input type="text" class="qs-ui-ipt" value="" /><a href="javascript:void(0)" class="qs-ui-btn ok">Ok</a><a class="qs-ui-btn cancel" href="javascript:void(0)">Cancel</a></div>');
            panel.setTitle('Confirm');
            panel.msg = panel.ctn.find('h5');
            panel.callback = function(){};
            var fnSubmit = function(){
                panel.hide();
                panel.callback(true);
                doNext();
            };
            //add by yuntao for change the text
            panel.setOkText = function(text){
                panel.ctn.find('.ok').text(text);
            };
            panel.setOkTip = function(text){
                panel.ctn.find('.ok')[0].title = text;
            };
            panel.setCancelText = function(text){
                panel.ctn.find('.cancel').text(text);
            };
            panel.setCancelTip = function(text){
                panel.ctn.find('.cancel')[0].title = text;
            };
            panel.ctn.find('.ok').click(fnSubmit);
            panel.ctn.find('.cancel').click(function(e){
                panel.hide();
                panel.callback(false);
                doNext();
            });
            panel.ctn.find('input').bind('keydown', function(e){
                if (e.keyCode == 13) {
                    fnSubmit();
                }
            });
            //panel.appendTo(document.body);
            Util.confirmObj = panel;
    };
    var show=function(msg,callback,config){ //config:{title:'Title'}
        if (!Util.confirmObj) {
            createConfirmObj();
        }
        if(!cacheRquest[msg]){
            cacheRquest[msg]={msg:msg,config:config,callbacks:[],closeCallbacks:[]};
            if(typeof callback=="function"){
                cacheRquest[msg].callbacks.push(callback);
            }
            if (config&&typeof config.closeCallback == 'function') {
                cacheRquest[msg].closeCallbacks.push(config.closeCallback);
            }
            if(!isLoading){
                Util.confirmObj.msg.html(msg);
                Util.confirmObj.msg.html(msg);
                Util.confirmObj.setTitle('Confirm');
                Util.confirmObj.setOkText('Ok');
                Util.confirmObj.setCancelText('Cancel');
                Util.confirmObj.setOkTip('');
                Util.confirmObj.setCancelTip('');
                if (config) {
                    if (config.title) {
                        Util.confirmObj.setTitle(config.title);
                    }
                    if (config.okText) {
                        Util.confirmObj.setOkText(config.okText);
                    }
                    if (config.cancelText) {
                        Util.confirmObj.setCancelText(config.cancelText);
                    }
                    if (config.okTipText) {
                        Util.confirmObj.setOkTip(config.okTipText);
                    }
                    if (config.cancelTipText) {
                        Util.confirmObj.setCancelTip(config.cancelTipText);
                    }
                    if (typeof config.closeCallback == 'function') {
                        with({message:msg}){
                            Util.confirmObj.setCloseCallback(function(){
                                while(cacheRquest[message].closeCallbacks.length>0){
                                    cacheRquest[message].closeCallbacks.shift()();
                                }
                                delete cacheRquest[message];
                                isLoading=false;
                                doNext();
                            });
                        }
                    }
                }
                if(typeof callback=="function"){
                    with({message:msg}){
                        Util.confirmObj.callback = function(b){
                            while(cacheRquest[message].callbacks.length>0){
                                cacheRquest[message].callbacks.shift()(b);
                            }
                            delete cacheRquest[message];
                            isLoading=false;
                        }
                    }
                }
                Util.confirmObj.show();
                Util.confirmObj.ctn.find('input').focus();
                isLoading=true;
            }
        }else{
            if(typeof callback=="function"){
                cacheRquest[msg].callbacks.push(callback);
            }
            if (config&&typeof config.closeCallback == 'function') {
                cacheRquest[msg].closeCallbacks.push(config.closeCallbacks);
            }
        }
    };
    return show;
}();
//Util.confirm();

Util.alertObj = null;
Util.alert = function(msg, config){ //config:{title:'Title'}
    if (!Util.alertObj) {
        var panel = new Util.panel();
        panel.jqDOM.addClass('rtq-alert rtq-a-c-p');
        panel.addToContent('<h5>Message</h5><div class="btn-ctn"><a href="javascript:void(0)" class="qs-ui-btn ok">OK</a></div>');
        panel.setTitle('Alert');
        panel.msg = panel.ctn.find('h5');
        
        var fnSubmit = function(){
            panel.hide();
            return;
        };
        
        panel.ctn.find('.ok').click(fnSubmit);
        
        panel.jqDOM.bind('keydown', function(e){
            if (e.keyCode == 13) {
                fnSubmit();
            }
        });
        Util.alertObj = panel;
    }
    Util.alertObj.msg.html(msg);
    if (config) {
        if (config.title) {
            Util.alertObj.setTitle(config.title);
        }
    }
    Util.alertObj.show();
};
//Util.alert();

Util.messageObj = null;
Util.message = function(msg){
    if (!Util.messageObj) {
        var panel = new Util.panel();
        panel.utilType = "message";
        panel.jqDOM.addClass('rtq-msg rtq-a-c-p');
        panel.addToContent('<h5>Message</h5>');
        panel.msg = panel.ctn.find('h5');
        var timer = null;
        
        panel.autoHide = function(dalay){
            if (timer) {
                clearTimeout(timer);
                timer = -1;
            }
            timer = setTimeout(function(){
                panel.hide();
            }, dalay || 8000);
        };
        
        panel.jqDOM.bind('keydown', function(e){
            if (e.keyCode == 13) {
                panel.hide();
            }
        });
        Util.messageObj = panel;
    }
    
    Util.messageObj.msg.html(msg);
    Util.messageObj.show();
    Util.messageObj.autoHide();
    
};